package com.rqbl.service.impl;

import com.rqbl.entity.Post;
import com.rqbl.mapper.PostMapper;
import com.rqbl.myutils.PageBean;
import com.rqbl.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 *  帖子类的实现
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    PostMapper postMapper;

    @Override
    public List<Post> getPostByPage(PageBean pageBean,Integer bid) {
        pageBean.setTotalSize(postMapper.selectAllPostCount());
        int totalPage = pageBean.getTotalSize() / pageBean.getPageSize();
        if(pageBean.getTotalSize() % pageBean.getPageSize() != 0) {
            totalPage++;
        }
        pageBean.setTotalPage(totalPage);
        return postMapper.selectAllPostPage((pageBean.getPage()-1)*pageBean.getPageSize(), pageBean.getPageSize(),bid);
    }

    @Override
    public Post getPostByPid(Integer pid) {
        return postMapper.selectPostByPid(pid);
    }

    @Override
    public Integer deletePostById(Integer pid) {
        return postMapper.deletePostById(pid);
    }

    @Override
    public List<Post> getPostByPageUid(PageBean pageBean, Integer uid) {
        pageBean.setTotalSize(postMapper.selectAllPostCount());
        int totalPage = pageBean.getTotalSize() / pageBean.getPageSize();
        if (pageBean.getTotalSize() % pageBean.getPageSize() != 0) {
            totalPage++;
            pageBean.setTotalPage(totalPage);
            return postMapper.selectAllPostPage((pageBean.getPage() - 1) * pageBean.getPageSize(), pageBean.getPageSize(), uid);
        }
        pageBean.setTotalPage(totalPage);
        return postMapper.selectAllPostPageUid((pageBean.getPage() - 1) * pageBean.getPageSize(), pageBean.getPageSize(), uid);
    }

    @Override
    public Integer addPost(Post post) {
        postMapper.insertPostInfo(post);
        System.out.println(post);
        return post.getPid();
    }

}
